package com.qqt.csr.common.vo.resp;

import com.alibaba.fastjson2.JSON;
import com.qqt.csr.common.exception.StatusCode;
import com.qqt.csr.common.utils.JsonUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;

import java.io.Serial;

/**
 * 请求响应对象
 *
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@Schema(description = "请求响应对象")
public class ObjectResponse<T> extends BaseResponse {
	@Serial
	private static final long serialVersionUID = -7816223140392699738L;
	@Schema(description = "响应数据")
	private T data;

	@Override
	public String toString() {
		return JsonUtil.toJson(this);
	}

	public static <T> ObjectResponse<T> common(int code, String message, T data) {
		ObjectResponse<T> result = new ObjectResponse<>();
		result.setCode(code);
		result.setData(data);
		result.setMessage(message);
		return result;
	}

	public static <T> ObjectResponse<T> success(T data, String message) {
		ObjectResponse<T> result = new ObjectResponse<>();
		result.setCode(StatusCode.SUCCESS.getCode());
		result.setData(data);
		result.setMessage(message);
		return result;
	}

	public static <T> ObjectResponse<T> success(T data) {
		ObjectResponse<T> result = new ObjectResponse<>();
		result.setCode(StatusCode.SUCCESS.getCode());
		result.setData(data);
		result.setMessage(StatusCode.SUCCESS.getMessage());
		return result;
	}

	public static ObjectResponse<String> success() {
		ObjectResponse<String> result = new ObjectResponse<>();
		result.setCode(StatusCode.SUCCESS.getCode());
		result.setMessage(StatusCode.SUCCESS.getMessage());
		return result;
	}

	/**
	 * 无数据失败返回
	 *
	 * @param code    返回码
	 * @param message 消息
	 * @return ignore
	 */
	public static ObjectResponse<String> failed(StatusCode code, String message) {
		ObjectResponse<String> result = new ObjectResponse<>();
		result.setCode(code.getCode());
		result.setData(code.getMessage());
		result.setMessage(message);

		return result;
	}

	public static ObjectResponse<String> failed(StatusCode code) {
		ObjectResponse<String> result = new ObjectResponse<>();
		result.setCode(code.getCode());
		result.setMessage(code.getMessage());
		return result;
	}

	public static <T> ObjectResponse<T> failed(StatusCode code, T data, String message) {
		ObjectResponse<T> result = new ObjectResponse<>();
		result.setCode(code.getCode());
		result.setData(data);
		result.setMessage(message);
		return result;
	}

	public static <T> ObjectResponse<T> failed(StatusCode code, T data) {
		ObjectResponse<T> result = new ObjectResponse<>();
		result.setCode(code.getCode());
		result.setData(data);
		result.setMessage(code.getMessage());
		return result;
	}

	public static ObjectResponse<Boolean> result(boolean result) {

		if (result) {
			return ObjectResponse.success(result);
		} else {
			return ObjectResponse.failed(StatusCode.INTERNAL_SERVER_ERROR, result);
		}

	}

}